package com.hexiaohe.springboot_vue_app.controller;

import com.hexiaohe.springboot_vue_app.entity.UserRole;
import com.hexiaohe.springboot_vue_app.service.UserRoleService;
import com.hexiaohe.springboot_vue_app.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userRole")
public class UserRoleController {
    @Autowired
    UserRoleService userRoleService;
    @PutMapping("/changeRole")
    public Result<?>changeRole(@RequestParam("userid")int userid,@RequestParam("newRole")int newRole){
        System.out.println("id:"+userid+",newrole:"+newRole);
        boolean flag = userRoleService.findByUserId(userid);
        UserRole userRole = new UserRole();
        userRole.setUserId(userid);
        userRole.setRoleId(newRole);
        int row =0;
        if (!flag){
            row = userRoleService.addUserRole(userRole);
            System.out.println("row"+row);
        }else {
            row = userRoleService.UpdateUserRole(userRole);
            System.out.println("row"+row);
        }
        if (row >0){
            return Result.success();
        }else {
            return Result.error("-1","修改永用户失败");
        }
    }

}
